<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\common\HttpCurl;
use app\logic\CommonLogic;
use app\logic\GoodsLogic;
use app\logic\PlayGoodsLogic;
use app\logic\ShopLogic;
use app\models\OrderGoods;
use Yii;


class GoodsController extends BaseController
{

    const LIMIT = 10;
	public function actionIndex()
	{
        $this->response(time());
	}

    /**
     * 商品列表
     * @apiDescription   by Jim
     * @api              {post} /goods/goods-list 商品列表
     * @apiName          goods-list
     * @apiVersion       1.0.0
     * @apiParam {int} orderBy 排序 总共有四种， 1 综合 2 销量高到低  3 价格低到高  4 价格高到低，默认是综合
     * @apiParam {int} category_id 分类ID，传空或传0为全部分类
     * @apiParam {int} page 页码，默认为1
     * @apiGroup         Goods
     * @apiSampleRequest /goods/goods-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
        "goods_id": 12, // int 商品Id
        "goods_pic": "https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3776766248,514215818&fm=173&app=25&f=JPEG?w=550&h=367&s=B9248F180C1448CE1E9870C00300F0A9", // 商品图片
        "goods_name": "商品标题商品标题商品标题", // 商品标题
        "goods_price": 12.00 // float 商品价格
      }]
     * @apiError         30000    获取失败
     */
    public function actionGoodsList(){
        $orderBy = $this->getQueryparams('orderBy',1);
        $category_id = $this->getQueryparams('category_id',0);
        $page = $this->getQueryparams('page',1);
        $goodsLogic = new GoodsLogic();
        $result = $goodsLogic->getAllGoodsByCategory($orderBy,$category_id,$page,self::LIMIT);
        foreach($result AS &$item){
            $item['goods_pic'] = Yii::$app->params['pic_domain'].$item['goods_pic'];
        }
        $this->response($result);
    }
    /**
     * 商品详情
     * @apiDescription   by Jim
     * @api              {post} /goods/goods-detail 商品详情
     * @apiName          goods-detail
     * @apiVersion       1.0.0
     * @apiParam {int} goods_id 商品ID
     * @apiGroup         Goods
     * @apiSampleRequest /goods/goods-detail?flagsign=1
     * @apiSuccessExample {json} 获取成功
     *
     {
        "goods_pics": ["http://img", "http://img"],
        "goods_name": "xxx", // 商品名
        "simple_desc": "舒适，便捷",  //商品名下的描述
        "sales_volume": 30, //int 销量
        "goods_price":90.00,
        "yanxuan_price":100.00,
        "sku":[{
        "sku_id":123,
        "sku_price":90.00,
        "yanxuan_price":100.00,
        //"sku_pic":"***",
        "sku_spec":[{
        "spec_name":"尺码",
        "spec_value":"大"
        }]
        }],
        "attr_list": [
        {
        "attrValue": "本品为人工填充例子，重量难免会有误差，敬请谅解",
        "attrName": "材质信息"
        }
        ],
        "goods_detail":""// 商品描述
     }
     * @apiError         30001    获取失败
     */
    public function actionGoodsDetail(){
        $goods_id = $this->getQueryparams('goods_id',0);
        $token = $this->getQueryparams('token','');
        if($token){
            $user = CacheHelper::get('user_token_'.$token);
            $this->user_id = $user['user_id'];
        }
        $goodsLogic = new GoodsLogic();
        $goods = $goodsLogic->getGoods($goods_id);
        if(!$goods){
            $this->error('30001','不存在该商品');
        }
        $picarr = explode(',',$goods['goods_pics']);
        foreach($picarr AS &$item){
            $item = Yii::$app->params['pic_domain'].$item;
        }
        $goods['goods_pics'] = $picarr;
        $favorite = $goodsLogic->getFavorite($this->user_id,$goods_id);
        $sku = $goodsLogic->getGoodsSku($goods_id);
        $skuids = array_column($sku,'yx_sku_id');
        $skuids = json_encode($skuids);
        /*$sku_stock = HttpCurl::post("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
        $sku_stock = json_decode($sku_stock,true);*/
        $spec = [];
        $finalsku = [];
        $spec_xz = [];
        foreach($sku AS $key=>&$value){
            $value['sku_pic'] = Yii::$app->params['pic_domain'].$value['sku_pic'];
            $fin_key = "";
            /*foreach ($sku_stock as $v){
                if($v['skuId']==$value['yx_sku_id'])
                    $value['stock'] = $v['inventory'];
            }*/
            $spec_xz_temp = [];
            $value['stock'] = $value['inventory'];
            foreach(json_decode($value['sku_spec'],true) AS $k=>$v){
                //var_dump($v);continue;
                $spec[$v['spec_name']][] = $v['spec_value'];
                $spec[$v['spec_name']] = array_unique(array_values($spec[$v['spec_name']]));
                $fin_key[] = $v['spec_value'];
                $spec_xz_temp[] = $v['spec_value'];
            }
            //continue;
            foreach ($spec_xz_temp AS $kk=>$vv){
                $temp = $spec_xz_temp;
                if(isset($spec_xz[$vv])){
                    array_splice($temp,$kk,1);
                    $spec_xz[$vv] = array_unique(array_merge($spec_xz[$vv],$temp));
                }else{
                    array_splice($temp,$kk,1);
                    $spec_xz[$vv] = $temp;
                }
            }
            sort($fin_key);
            $fin_key = implode(',',$fin_key);
            $value['sku_spec'] = json_decode($value['sku_spec'],true);
            $finalsku[$fin_key] = $value;
            //$value['choose_spec'] = $fin_key;
        }
        if($favorite){
            $goods['is_favorite'] = 1;
        }else{
            $goods['is_favorite'] = 0;
        }
        $goods['sku'] = $finalsku;
        $goods['spec'] = $spec;
        $goods['spec_xz'] = $spec_xz;
        //$goods['goods_pics'] = explode(',',$goods['goods_pics']);
        $goods['attr_list'] = json_decode($goods['attr_list'],true);
        $goods['goods_detail'] = preg_replace("/<p.*?>|<\/p>/is","", $goods['goods_detail']);;
        $this->response($goods);
    }
}
